---
title: "How do populist radical right parties differentiate their appeal? Evidence from the media strategy of the Hungarian Jobbik party"
subtitle: "Replication File"
author: "Endre Borbáth, Theresa Gessler"
date: "Last compiled: `r Sys.Date()`"
output: 
    html_document:
        toc: true
        code_folding: hide
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,cache=T,warnings=F,message=F,warning=F)

p_needed <- c("tidyverse", "quanteda","lubridate","stringr", "knitr", "kableExtra", "readxl", "caret","e1071", "haven","car","foreign","tidyr","readxl","reshape","zoo","readr","stm","tidytext", "effects", "texreg", "ggupset")
lapply(p_needed, require, character.only = TRUE)

sessionInfo()
```

# Data preparation

```{r data}
source("dictionary_final2.R",encoding="UTF-8")

load("data/joined_tokens.RData")
joined_tokens<-tokens_replace(joined_tokens,phrase(c("nemzeti* együttmuködés","rendszeres")),phrase(c("NER","gyakori")))
docvars(joined_tokens) <- docvars(joined_tokens) %>% mutate(drop=ifelse(data_source=="jobbik",duplicated(long_text),FALSE))
joined_tokens <- tokens_subset(joined_tokens,drop==F)
load("data/fidesz_tokens.RData")
fidesz_tokens<-tokens_replace(fidesz_tokens,phrase(c("nemzeti* együttmuködés","rendszeres*","állampolgárság*")),phrase(c("NER","gyakori","apsag")))

resulttoks<-tokens_lookup(joined_tokens,dictionary=topic_dictionary,nomatch="OTHER")

results_df_dum <- dfm(resulttoks) %>%
  dfm_weight("boolean") %>% 
  convert("data.frame") %>% 
  bind_cols(docvars(resulttoks)) %>% 
  group_by(data_source,ym) %>% 
  mutate(m_nativism=mean(nativism),
         m_people_centrism=mean(people_centrism),
         m_anti_elitism=mean(anti_elitism)) %>% ungroup() %>%
  mutate(data_source=ifelse(data_source=="jobbik","Press Releases",data_source)) %>%
  mutate(populism=ifelse(anti_elitism>=1 & people_centrism>=1,1,0)) %>%
  mutate(data_source=case_when(data_source=="alfahir"~"Alfahir.hu",
                               data_source=="fb_jobbik"~"Facebook - Jobbik page",
                               data_source=="fb_vona"~"Facebook - Vona page",
                               data_source=="kurucinfo"~"Kuruc.info",TRUE~data_source)) %>% 
  group_by(data_source,ym) %>% 
  mutate(m_populism=mean(populism))

results_df_prop <- dfm(resulttoks) %>% 
  dfm_weight("prop") %>%
  convert("data.frame") %>% 
  bind_cols(docvars(resulttoks)) %>% 
  group_by(data_source,ym) %>% 
  mutate(m_nativism=mean(nativism),
         m_people_centrism=mean(people_centrism),
         m_anti_elitism=mean(anti_elitism)) %>% ungroup() %>%
  mutate(data_source=ifelse(data_source=="jobbik","Press Releases",data_source)) %>%
  mutate(populism=ifelse(anti_elitism>0 & people_centrism>0,anti_elitism+people_centrism,0)) %>%
  mutate(data_source=case_when(data_source=="alfahir"~"Alfahir.hu",
                               data_source=="fb_jobbik"~"Facebook - Jobbik page",
                               data_source=="fb_vona"~"Facebook - Vona page",
                               data_source=="kurucinfo"~"Kuruc.info",TRUE~data_source)) %>% 
  group_by(data_source,ym) %>% 
  mutate(m_populism=mean(populism))

results_df_dum$data_source <- factor(results_df_dum$data_source,
                                     levels = c("Kuruc.info", "Alfahir.hu", "Facebook - Jobbik page",
                                                "Facebook - Vona page", "Press Releases"))



results_df_prop$data_source <- factor(results_df_prop$data_source,
                                     levels = c("Kuruc.info", "Alfahir.hu", "Facebook - Jobbik page",
                                                "Facebook - Vona page", "Press Releases"))

save(results_df_dum,results_df_prop,file="data/analysis_df.RData")


fidesz_resulttoks<-tokens_lookup(fidesz_tokens,dictionary=topic_dictionary,nomatch="OTHER")
fidesz_df_dum<-dfm(fidesz_resulttoks) %>% dfm_weight("boolean") %>% convert("data.frame") %>% bind_cols(docvars(fidesz_resulttoks)) %>% 
  group_by(ym) %>% 
  mutate(m_nativism=mean(nativism),
         m_people_centrism=mean(people_centrism),
         m_anti_elitism=mean(anti_elitism)) %>% ungroup() %>%
  mutate(populism=ifelse(anti_elitism>=1 & people_centrism>=1,1,0)) %>% 
  group_by(ym) %>% 
  mutate(m_populism=mean(populism))

fidesz_df_prop<-dfm(fidesz_resulttoks)  %>% 
  dfm_weight("prop")  %>%
  convert("data.frame") %>% 
  bind_cols(docvars(fidesz_resulttoks)) %>% 
  group_by(ym) %>% 
  mutate(m_nativism=mean(nativism),
         m_people_centrism=mean(people_centrism),
         m_anti_elitism=mean(anti_elitism)) %>% ungroup() %>%
  mutate(populism=ifelse(anti_elitism>0 & people_centrism>0,anti_elitism+people_centrism,0)) %>%
  group_by(ym) %>% 
  mutate(m_populism=mean(populism))

fidesz_monthly <- fidesz_df_dum %>% group_by(ym) %>%
  select(anti_elitism,nativism, people_centrism,nativism_appendix,populism) %>%
  summarize_all(mean) %>%
  select(anti_elitism_f=anti_elitism,nativism_f=nativism,populism_f=populism,nativism_appendix_f=nativism_appendix,ym)

jobbik_monthly <- results_df_dum %>% group_by(ym,data_source) %>%
  select(anti_elitism,nativism, people_centrism,nativism_appendix,populism) %>%
  summarize_all(mean)


fidesz_monthly_prop <- fidesz_df_prop %>% group_by(ym) %>%
  select(anti_elitism,nativism, people_centrism,nativism_appendix,populism) %>%
  summarize_all(mean) %>%
  select(anti_elitism_f=anti_elitism,nativism_f=nativism,populism_f=populism,nativism_appendix_f=nativism_appendix,ym)

jobbik_monthly_prop <- results_df_prop %>% group_by(ym,data_source) %>%
  select(anti_elitism,nativism, people_centrism,nativism_appendix,populism) %>%
  summarize_all(mean)

monthly <- full_join(fidesz_monthly,jobbik_monthly)
monthly_prop <- full_join(fidesz_monthly_prop,jobbik_monthly_prop)
#save(monthly, file="monthly.RData")
haven::write_dta(monthly,path="data/monthly.dta")
haven::write_dta(monthly_prop,path="data/monthly_prop.dta")


fidesz_quarterly <- fidesz_df_dum %>%
  mutate(quarter=cut(date,"quarter")) %>%
  group_by(quarter,data_source) %>%
  select(anti_elitism,nativism, people_centrism,nativism_appendix,populism) %>%
  summarize_all(mean) %>%
  select(anti_elitism_f=anti_elitism,nativism_f=nativism,populism_f=populism,nativism_appendix_f=nativism_appendix,quarter,data_source)
fidesz_quarterly_wide <- fidesz_quarterly %>%
  mutate(data_source=ifelse(data_source=="fidesz","pm_fidesz",data_source)) %>% 
  pivot_wider(names_from=data_source,values_from=c(anti_elitism_f,nativism_f,populism_f,nativism_appendix_f))

jobbik_quarterly <- results_df_dum %>%
    mutate(quarter=cut(date,"quarter")) %>%
group_by(quarter,data_source) %>%
  select(anti_elitism,nativism, people_centrism,nativism_appendix,populism) %>%
  summarize_all(mean)
jobbik_expanded <-jobbik_quarterly %>% full_join(expand_grid(quarter=unique(jobbik_quarterly$quarter),data_source=unique(jobbik_quarterly$data_source)))

quarterly <- full_join(jobbik_expanded,fidesz_quarterly_wide) %>%
            filter(is.na(data_source)==F) %>% ungroup() 

rm(jobbik_expanded)
rm(fidesz_quarterly_wide)
#save(quarterly, file="../data/quarterly.RData")
haven::write_dta(quarterly,path="data/quarterly.dta")

fidesz_quarterly_cont <- fidesz_df_prop %>%
  mutate(quarter=cut(date,"quarter")) %>%
  group_by(quarter,data_source) %>%
  select(anti_elitism,nativism, people_centrism,nativism_appendix,populism) %>%
  summarize_all(mean) %>%
  select(anti_elitism_f=anti_elitism,nativism_f=nativism,populism_f=populism,nativism_appendix_f=nativism_appendix,quarter,data_source)
fidesz_quarterly_wide <- fidesz_quarterly_cont %>%
  mutate(data_source=ifelse(data_source=="fidesz","pm_fidesz",data_source)) %>% 
  pivot_wider(names_from=data_source,values_from=c(anti_elitism_f,nativism_f,populism_f,nativism_appendix_f))

jobbik_quarterly_cont <- results_df_prop %>%
    mutate(quarter=cut(date,"quarter")) %>%
group_by(quarter,data_source) %>%
  select(anti_elitism,nativism, people_centrism,nativism_appendix,populism) %>%
  summarize_all(mean)
jobbik_expanded <-jobbik_quarterly_cont %>% full_join(expand_grid(quarter=unique(jobbik_quarterly$quarter),data_source=unique(jobbik_quarterly$data_source)))

quarterly_cont <- full_join(jobbik_expanded,fidesz_quarterly_wide) %>%
            filter(is.na(data_source)==F) %>% ungroup() 

#save(quarterly_cont, file="../data/quarterly_cont.RData")
haven::write_dta(quarterly_cont,path="data/quarterly_prop.dta")

```
## Dictionary

```{r}
topic_dictionary
```

# Main Text

## Figure 1: Documents with a nativist and populist appeal on each platform 

```{r}
dat <- results_df_dum %>% ungroup() %>%
  mutate(data_source=as.character(as_factor(data_source))) %>% 
  select(nativism, populism, data_source) %>% 
  group_by(data_source) %>% 
  mutate(nativism=mean(nativism, na.rm=TRUE),
         populism=mean(populism, na.rm=TRUE)) %>% 
  unique(.) %>% 
  ungroup(.) %>% 
  pivot_longer(cols=c(nativism, populism), names_to="type") %>% 
  arrange(type, value) %>% 
  mutate(order=ifelse(type=="nativism", row_number(), NA)) %>% 
  group_by(data_source) %>% 
  mutate(order=mean(order, na.rm = TRUE))

dat_plot <- ggplot(dat, aes(x=reorder(data_source, -order), y=value, fill=type,alpha=type)) +
  geom_bar(stat="identity", position="dodge") +
  xlab("") + ylab("Share of documents") +
  scale_fill_manual(values = c("nativism"="#000000","populism"="#002366")) +
  scale_alpha_manual(values=c(1,0.5))+
  theme_minimal() +
  theme(legend.position="bottom",legend.title=element_blank(),axis.text=element_text(size=8))

dat_plot

ggsave(filename="figure1.png",
       path="figures",
       plot=dat_plot,
       device="png",
       dpi=300, 
       width=16,
       height=12,
       scale = 1.3,
       units="cm")
```

## Figure 2: The share of documents over time with a populist or nativist appeal on the different platforms

```{r,fig.width=8,fig.height=12}
figure2 <- results_df_dum %>%
  ggplot(aes(x=date))+
  geom_smooth(span=0.2,aes(y=nativism,color="nativism", linetype="nativism",fill="nativism"),method="loess", size=0.6)+
  geom_smooth(span=0.2,aes(y=populism,color="populism", linetype="populism",fill="populism"),method="loess", size=0.6)+
  scale_x_date(breaks="years",date_labels="%Y")+
  #geom_smooth()+
  facet_wrap("data_source",ncol=2, nrow=3)+theme_minimal()+
  xlab("") + ylab("Share of documents") +
  geom_vline(xintercept=c(as.Date("09.04.2006","%d.%m.%Y"),as.Date("11.04.2010","%d.%m.%Y"),as.Date("06.04.2014","%d.%m.%Y"),as.Date("08.04.2018","%d.%m.%Y")),linetype="dashed") +
  scale_colour_manual("",   limits=c("nativism", "populism"),
                      breaks = c("nativism", "populism"),
                      values = c("#002366", "black")) + 
  scale_fill_manual("",   limits=c("nativism", "populism"),
                      breaks = c("nativism", "populism"),
                      values = c("#002366", "black")) +
  scale_linetype_manual("",   limits=c("nativism", "populism"),
                        breaks = c("nativism", "populism"),
                        values = c("solid","dashed")) +
  guides(color=guide_legend(override.aes=list(fill=NA))) +
  theme(axis.title.x=element_blank(),
        axis.text.x = element_text(angle = 45, hjust = 1),
        legend.title = element_blank(),
        legend.position="bottom", 
        legend.direction="horizontal",
        legend.margin=margin(t = 0, unit='cm'),
        legend.key.width = unit(2.2,"cm"))+
  coord_cartesian(ylim=c(0,1))

figure2

ggsave(filename="figure2.png",
       path="figures",
       plot=figure2,
       device="png",
       dpi=300, 
       width=13,
       height=16,
       scale = 1.3,
       units="cm")
```

## Figure 3: Over time share of documents with a nativist appeal on the different platforms compared to the share of nativism in Fidesz’ press releases


```{r}
fidesz_dat <- fidesz_df_dum %>% filter(date>as.Date("2006-02-01")) %>% select(-data_source)

nativism_fidesz <- results_df_dum %>%
  ggplot(aes(x=date))+
  #geom_point(aes(m_nativism, x=as.Date(paste0("20",ym,"-01"),"%Y-%m-%d")), shape=1, color="gray50")+
  geom_smooth(span=0.2,aes(y=nativism,color="nativism",linetype="nativism",fill="nativism"),method="loess", size=0.6)+
  geom_smooth(data=fidesz_dat,span=0.2,aes(y=nativism,color="Fidesz nativism",linetype="Fidesz nativism",fill="Fidesz nativism"),method="loess", size=0.6)+
  scale_x_date(breaks="years",date_labels="%Y")+
  facet_wrap("data_source",ncol=2,nrow=3)+theme_minimal()+
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
    axis.title.x=element_blank(),
    legend.position="bottom")+
  xlab("") + ylab("Share of documents") +
  coord_cartesian(ylim=c(0, 1))+
    scale_colour_manual("", labels=c("Jobbik nativism", "Fidesz nativism"),
                      breaks = c("nativism", "Fidesz nativism"),
                      values = c("#002366","black")) +
    scale_fill_manual("",   labels=c("Jobik nativism", "Fidesz nativism"),
                      breaks = c("nativism", "Fidesz nativism"),
                      values = c("#002366","black")) +
  scale_linetype_manual("",   labels=c("Jobbik nativism", "Fidesz nativism"),
                        breaks = c("nativism", "Fidesz nativism"),
                        values = c("solid","dashed")) +
  geom_vline(xintercept=c(as.Date("09.04.2006","%d.%m.%Y"),as.Date("11.04.2010","%d.%m.%Y"),as.Date("06.04.2014","%d.%m.%Y"),as.Date("08.04.2018","%d.%m.%Y")),linetype="dashed")+
  guides(fill=F,color=guide_legend(override.aes=list(fill=NA)))+
  theme(axis.title.x=element_blank(),
        axis.text.x = element_text(angle = 45, hjust = 1),
        legend.title = element_blank(),
        legend.position="bottom", 
        legend.direction="horizontal",
        legend.margin=margin(t = 0, unit='cm'),
        legend.key.width = unit(2.2,"cm"))

nativism_fidesz

ggsave(filename="appb_figure4.png",
       path="figures",
       plot=nativism_fidesz,
       device="png",
       dpi=300, 
       width=13,
       height=16,
       scale = 1.3,
       units="cm")
```

## Table 1: Level of nativism in Jobbik’s appeal (quarterly level, TSCS regression)

- see quarterly.do

## Figure 4: Marginal effects: Jobbik’s nativism on the different platforms as a function of the share of undecided voters and the party’s popularity

- see quarterly.do

# Appendix A: Dictionary for populism and nativism

## Figure 1: Number of documents classified based on the dictionaries (binary classification)

```{r}
library(ggupset)

upset_fig <- results_df_dum %>% ungroup() %>%
  select(nativism,anti_elitism,people_centrism,other,doc_id) %>%
  mutate(other=ifelse(nativism+anti_elitism+people_centrism==0,1,0)) %>%
  dplyr::rename(Nativism=nativism,
         `Anti-elitism`=anti_elitism,
         `People-centrism`=people_centrism,
         Other=other) %>% 
  pivot_longer(-doc_id,"ideology","value") %>%
  filter(value!=0) %>%
  group_by(doc_id) %>%
  summarize(ideology = list(ideology)) %>%
  ggplot(aes(x=ideology)) +
    geom_bar() +
    ylab("Count of documents") +
    xlab("") +
    theme_bw() +
    scale_x_upset()

upset_fig

ggsave(filename="appd_upset_fig.png",
       path="figures",
       plot=upset_fig,
       device="png",
       dpi=300, 
       width=16,
       height=12,
       scale = 1.3,
       units="cm")
```

## Figure 2: Documents with a nativist and populist appeal on the different platforms  (alternative dictionary)


```{r}
robusttoks <- tokens_lookup(joined_tokens,dictionary=robustness_dictionary)

robust_df_dum <- dfm(robusttoks) %>%
  dfm_weight("boolean") %>% 
  convert("data.frame") %>% 
  bind_cols(docvars(robusttoks)) %>% 
  group_by(data_source,ym) %>% 
  mutate(m_nativism=mean(nativism),
         m_people_centrism=mean(people_centrism),
         m_anti_elitism=mean(anti_elitism)) %>% ungroup() %>%
  mutate(data_source=ifelse(data_source=="jobbik","Press Releases",data_source)) %>%
  mutate(populism=ifelse(anti_elitism>=1 & people_centrism>=1,1,0)) %>%
  mutate(data_source=case_when(data_source=="alfahir"~"Alfahir.hu",
                               data_source=="fb_jobbik"~"Facebook - Jobbik page",
                               data_source=="fb_vona"~"Facebook - Vona page",
                               data_source=="kurucinfo"~"Kuruc.info",TRUE~data_source)) %>% 
  group_by(data_source,ym) %>% 
  mutate(m_populism=mean(populism))


robust_df_dum$data_source <- factor(robust_df_dum$data_source,
                                     levels = c("Kuruc.info", "Alfahir.hu", "Facebook - Jobbik page",
                                                "Facebook - Vona page", "Press Releases"))


```

```{r}
dat_new <- robust_df_dum %>% ungroup() %>%
  mutate(data_source=as.character(as_factor(data_source))) %>% 
  select(nativism, populism, data_source) %>% 
  group_by(data_source) %>% 
  mutate(nativism=mean(nativism, na.rm=TRUE),
         populism=mean(populism, na.rm=TRUE)) %>% 
  unique(.) %>% 
  ungroup(.) %>% 
  pivot_longer(cols=c(nativism, populism), names_to="type") %>% 
  arrange(type, value) %>% left_join(dat %>% select(-value))

dat_plot <- ggplot(dat_new, aes(x=reorder(data_source, -order), y=value, fill=type,alpha=type)) +
  geom_bar(stat="identity", position="dodge") +
  xlab("") + ylab("Share of documents") +
  scale_fill_manual(values = c("nativism"="#000000","populism"="#002366")) +
  scale_alpha_manual(values=c(1,0.5))+
  theme_minimal() +
  theme(legend.position="bottom",legend.title=element_blank(),axis.text=element_text(size=8))

dat_plot

ggsave(filename="appnew_figure1.png",
       path="figures",
       plot=dat_plot,
       device="png",
       dpi=300, 
       width=16,
       height=12,
       scale = 1.3,
       units="cm")
```


## Figure 3: The share of documents over time with a populist or nativist appeal on the different platforms (alternative dictionary)


```{r,fig.width=8,fig.height=12}
figure2 <- robust_df_dum %>%
  ggplot(aes(x=date))+
  geom_smooth(span=0.2,aes(y=nativism,color="nativism", linetype="nativism",fill="nativism"),method="loess", size=0.6)+
  geom_smooth(span=0.2,aes(y=populism,color="populism", linetype="populism",fill="populism"),method="loess", size=0.6)+
  scale_x_date(breaks="years",date_labels="%Y")+
  #geom_smooth()+
  facet_wrap("data_source",ncol=2, nrow=3)+theme_minimal()+
  xlab("") + ylab("Share of documents") +
  geom_vline(xintercept=c(as.Date("09.04.2006","%d.%m.%Y"),as.Date("11.04.2010","%d.%m.%Y"),as.Date("06.04.2014","%d.%m.%Y"),as.Date("08.04.2018","%d.%m.%Y")),linetype="dashed") +
  scale_colour_manual("",   limits=c("nativism", "populism"),
                      breaks = c("nativism", "populism"),
                      values = c("#002366", "black")) + 
  scale_fill_manual("",   limits=c("nativism", "populism"),
                      breaks = c("nativism", "populism"),
                      values = c("#002366", "black")) +
  scale_linetype_manual("",   limits=c("nativism", "populism"),
                        breaks = c("nativism", "populism"),
                        values = c("solid","dashed")) +
  guides(color=guide_legend(override.aes=list(fill=NA))) +
  theme(axis.title.x=element_blank(),
        axis.text.x = element_text(angle = 45, hjust = 1),
        legend.title = element_blank(),
        legend.position="bottom", 
        legend.direction="horizontal",
        legend.margin=margin(t = 0, unit='cm'),
        legend.key.width = unit(2.2,"cm"))+
  coord_cartesian(ylim=c(0,1))

figure2

ggsave(filename="appnew_figure2.png",
       path="figures",
       plot=figure2,
       device="png",
       dpi=300, 
       width=13,
       height=16,
       scale = 1.3,
       units="cm")
```


# Appendix B: Additional figures and tables

## Table 1: Descriptive Features

```{r,fig.width=8,fig.height=12}
results_df_dum <- results_df_dum   %>% mutate(data_type=case_when(data_source=="Kuruc.info"|data_source=="Alfahir.hu"~"partisan media",
                             data_source=="Facebook - Jobbik page"|data_source=="Facebook - Vona page"~"FB",
                             data_source=="Press Releases"~"Press releases"))

endre_table<-results_df_dum %>% 
  mutate(an=ifelse(nativism==1 & anti_elitism==1,1,0)) %>%
  mutate(np=ifelse(nativism==1 & people_centrism==1,1,0)) %>% 
  mutate(ap=ifelse(people_centrism==1 & anti_elitism==1,1,0)) %>%
  mutate(anp=ifelse(nativism==1 & anti_elitism==1 & people_centrism==1,1,0)) %>%
  group_by(data_source) %>% add_count() %>% mutate(first=min(date),last=max(date)) %>%
  mutate(an=sum(an),np=sum(np),ap=sum(ap),anp=sum(anp),nat=sum(nativism),ant=sum(anti_elitism),peo=sum(people_centrism)) %>%
  select(nat,ant,peo,an,np,ap,anp,first,last,n,word_count) %>% summarize_all(mean) %>%
mutate(a_n=paste0(an," (",round(an/n*100,digits=0),"%)")) %>%
mutate(word_count=round(word_count,0)) %>%
mutate(n_p=paste0(np," (",round(np/n*100,digits=0),"%)")) %>%
mutate(a_p=paste0(ap," (",round(ap/n*100,digits=0),"%)")) %>%
mutate(a_n_p=paste0(anp," (",round(anp/n*100,digits=0),"%)")) %>%
mutate(nativism=paste0(nat," (",round(nat/n*100,digits=0),"%)")) %>%
mutate(anti_elitism=paste0(ant," (",round(ant/n*100,digits=0),"%)")) %>%
mutate(people_centrism=paste0(peo," (",round(peo/n*100,digits=0),"%)"))   %>%
mutate(first=format.Date(first,"%d.%m.%Y")) %>%
mutate(last=format.Date(last,"%d.%m.%Y")) %>%
  select(data_source,first,last,n,anti_elitism,people_centrism,nativism,a_p,a_n, n_p, a_n_p,word_count) 


table2 <- endre_table %>% 
  select(first,last,n,word_count,a_p,nativism,a_n_p) %>% t() 

rownames(table2) <- c("First observation","Last observation","Total number of documents","Average length of document (# of words)","Populist","Nativist","Populism and Nativism")

table2 %>% kable(caption="Descriptive features of the five corpuses",col.names=levels(endre_table$data_source)) %>% kable_styling() %>%
  group_rows("Period",1,2) %>% group_rows("Corpus characteristics",3,4) %>%
  group_rows("Number of classified documents (share in corpus in parenthesis)",5,6) %>%
  group_rows("Cooccurence (share in corpus in parenthesis)",7,7)
```

## Figure 1: Marginal effects of Fidesz’ nativism on Jobbik’s nativism

- see quarterly.do

## Figure 2: Marginal effects of the share of undecided voters on Jobbik’s nativism

- see quarterly.do

## Figure 3: Electoral share of Jobbik in public opinion polls and national elections


```{r}
polls <- haven::read_dta("data/joined_stata.dta") %>%
  mutate(date=as.Date(paste0(Year,"/",Month,"/1")))
polls_new<-xlsx::read.xlsx("data/jobbik_polls.xlsx",1) 

polls_new_all=polls_new %>% select(starts_with("Medi"),Ipsos,Tarki,Repub,Public,Date) %>%
    tidyr::gather("institute","value",-Date) %>%
  mutate(value=as.numeric(value)) %>%
  mutate(Date=as.Date(paste0(Date,"/01")))

polls_new_all= polls %>% select(Date=date,value=Jobbik) %>% mutate(institute="Tarki") %>% bind_rows(polls_new_all)

popularity <- polls_new_all %>% 
    filter(institute!="nezop" & institute!="iranytu") %>%
    ggplot(aes(x=Date,y=value))+geom_point(alpha=0.4)+
    #geom_vline(xintercept=c(as.Date("07.06.2009","%d.%m.%Y"),as.Date("25.05.2014","%d.%m.%Y")),linetype="dashed")+
    geom_vline(xintercept=c(as.Date("09.04.2006","%d.%m.%Y"),as.Date("11.04.2010","%d.%m.%Y"),as.Date("06.04.2014","%d.%m.%Y"),as.Date("08.04.2018","%d.%m.%Y")),linetype="dashed")+
    xlab("")+ylab("Electoral share")+
    geom_smooth(span=0.3,color="#002366",fill="#002366", size=0.6)+theme_minimal()+
    geom_point(aes(x=as.Date("08.04.2006","%d.%m.%Y"),y=2.02),shape=17,size=3)+
    geom_point(aes(x=as.Date("09.04.2018","%d.%m.%Y"),y=19.06),shape=17,size=3)+
    geom_point(aes(x=as.Date("06.04.2014","%d.%m.%Y"),y=20.22),shape=17,size=3)+
    geom_point(aes(x=as.Date("11.04.2010","%d.%m.%Y"),y=16.67),shape=17,size=3)+
    coord_cartesian(ylim=c(0,22))+
    scale_x_date(breaks="years",date_labels="%Y") 

popularity

ggsave(filename="figure3.png",
       path="figures",
       plot=popularity,
       device="png",
       dpi=300, 
       width=16,
       height=10,
       scale = 1.3,
       units="cm")

```


# Appendix C: Content based transformation of nativism and populism

## Figure 1: Over time prevalence of the most prevalent keywords associated with anti-Roma and anti-immigration mobilization


```{r,fig.width=8,fig.height=12}
by_word_dict <- dictionary(list(
  muszlim=c("muszlim*"),
  zsido=c("zsidó*"),
  izrael=c("izrael*"),
  koser=c("kóser*"),
  cigany=c("cigány*"),
  migrans=c("migráns*"),
  menekult=c("menekült*"),
  betelepites=c("betelepítés*"),
  neger=c("néger*"),
  Trianon=c("Trianon*"),
  Horthy=c("Horthy*"),
  Garda=c("Gárda*"),
  magyarsag=c("magyarság*"),
  Karpatmedence=c("Kárpát-medence*"), 
  elcsatoltterulet=c("elcsatolt terület*"),
  elszakitottterulet=c("elszakított terület*"),
  anyaorszag=c("anyaország*"),
  turul=c("turul*"),
  szegreg=c("szegreg*"),
  bevandorl=c("bevándorl*"),
  elciganyos=c("elcigányos*"),
  roma=c("roma"),
  demografi=c("demográfi*"),
  elszakitottmagyar=c("elszakított magyar*"),
  elszakitottnemzet=c("elszakított nemzet*"),
  elszakitottorszag=c("elszakított ország*"),
  arab=c("arab*"),
  hatar=c("határ"),
  hatart=c("határt"),
  hatara=c("határa"),
  hataron=c("határon"),
  hatarzar=c("határzár*"),
  hataror=c("határőr*"),
  idegen=c("idegen"),
  idegenek=c("idegenek"),
  holokauszt=c("holokauszt*"),
  holokam=c("holokam*"),
  feherember=c("fehér ember*"))
)


by_word<-tokens_lookup(joined_tokens,by_word_dict)

by_word_df<-dfm(by_word) %>% dfm_weight("boolean") %>% convert("data.frame") %>% bind_cols(docvars(by_word))

by_word_df <- by_word_df %>% 
  mutate(data_source=ifelse(data_source=="jobbik","Press Releases",
                            ifelse(data_source=="alfahir", "Alfahir.hu",
                                   ifelse(data_source=="fb_jobbik", "Facebook - Jobbik page",
                                          ifelse(data_source=="fb_vona", "Facebook - Vona page",
                                                 ifelse(data_source=="kurucinfo", "Kuruc.info",
                                                        data_source))))))  %>%
  mutate(data_source=factor(data_source,
                                     levels = c("Kuruc.info", "Alfahir.hu", "Facebook - Jobbik page",
                                                "Facebook - Vona page", "Press Releases")))
  

drivers <- ggplot(by_word_df,aes(x=date))+
  geom_smooth(aes(y=muszlim),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=zsido),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=izrael),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=koser),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=betelepites),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=neger),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=trianon),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=horthy),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=magyarsag),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=karpatmedence),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=elcsatoltterulet),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=elszakitottterulet),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=anyaorszag),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=turul),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=szegreg),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=elciganyos),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=demografi),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=elszakitottmagyar),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=elszakitottnemzet),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=elszakitottorszag),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=arab),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=hatar),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=hatart),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=hatara),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=hataron),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=hatarzar),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=hataror),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=idegen),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=idegenek),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=holokauszt),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=holokam),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=feherember),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  geom_smooth(aes(y=garda,color="Roma: gárda"),method="loess",se=F,size=0.6)+
  geom_smooth(aes(y=cigany,color="Roma: cigány"),method="loess",se=F,size=0.6)+
  geom_smooth(aes(y=migrans,color="migration: migrans"),method="loess",se=F,size=0.6)+
  geom_smooth(aes(y=menekult,color="migration: menekült"),method="loess",se=F,size=0.6)+
  geom_smooth(aes(y=bevandorl,color="migration: bevándorlo"),method="loess",se=F,size=0.6)+
  geom_smooth(aes(y=roma,color="Roma: roma"),method="loess",color=alpha("grey",0.7),se=F,size=0.6)+
  facet_wrap(~data_source, ncol=2) + xlab("") + ylab("share of documents")+
  scale_x_date(breaks="years",date_labels="%Y")+
  theme_minimal() +
  scale_color_manual(values=c('#002366','#1b9e77','#d95f02','#7570b3','#e7298a','#66a61e'))+
  geom_vline(xintercept=c(as.Date("09.04.2006","%d.%m.%Y"),as.Date("11.04.2010","%d.%m.%Y"),as.Date("06.04.2014","%d.%m.%Y"),as.Date("08.04.2018","%d.%m.%Y")),linetype="dashed")+
  guides(color=guide_legend(override.aes=list(fill=NA),nrow=2,byrow=TRUE)) +
  theme(axis.title.x=element_blank(),
        axis.text.x = element_text(angle = 45, hjust = 1),
        legend.title = element_blank(),
        legend.position="bottom", 
        legend.direction="horizontal",
        legend.margin=margin(t = 0, unit='cm'),
        legend.key.width = unit(2.2,"cm"))

drivers

ggsave(filename="appc_figure1.png",
       path="figures",
       plot=drivers,
       device="png",
       dpi=300, 
       width=13,
       height=16,
       scale = 1.3,
       units="cm")
```

## Figure 2: Over time prevalence of documents classified as having a people centrist or an anti-establishment appeal

```{r}
both <- results_df_dum %>%
  ggplot(aes(x=date))+
  geom_smooth(span=0.2,aes(y=people_centrism,color="people-centrism", linetype="people-centrism",fill="people-centrism"),method="loess", size=0.6)+
  geom_smooth(span=0.2,aes(y=anti_elitism,color="anti-elitism", linetype="anti-elitism",fill="anti-elitism"),method="loess", size=0.6)+
  scale_x_date(breaks="years",date_labels="%Y")+
  #geom_smooth()+
  facet_wrap("data_source",ncol=2, nrow=3)+theme_minimal()+
  xlab("") + ylab("Share of documents") +
  geom_vline(xintercept=c(as.Date("09.04.2006","%d.%m.%Y"),as.Date("11.04.2010","%d.%m.%Y"),as.Date("06.04.2014","%d.%m.%Y"),as.Date("08.04.2018","%d.%m.%Y")),linetype="dashed") +
  scale_colour_manual("",   limits=c("anti-elitism", "people-centrism"),
                      breaks = c("anti-elitism", "people-centrism"),
                      values = c("#002366", "black")) + 
  scale_fill_manual("",   limits=c("anti-elitism", "people-centrism"),
                      breaks = c("anti-elitism", "people-centrism"),
                      values = c("#002366", "black")) +
  scale_linetype_manual("",   limits=c("anti-elitism", "people-centrism"),
                        breaks = c("anti-elitism", "people-centrism"),
                        values = c("solid","dashed")) +
  guides(color=guide_legend(override.aes=list(fill=NA))) +
  theme(axis.title.x=element_blank(),
        axis.text.x = element_text(angle = 45, hjust = 1),
        legend.title = element_blank(),
        legend.position="bottom", 
        legend.direction="horizontal",
        legend.margin=margin(t = 0, unit='cm'),
        legend.key.width = unit(2.2,"cm"))+
  coord_cartesian(ylim=c(0,1))

both
```


# Appendix D: Distribution of documents classified with a proportional measure


## Figure 1: Documents with a nativist and populist appeal on the different platforms  (proportional)

```{r}
dat_new <- results_df_prop %>% ungroup() %>%
  mutate(data_source=as.character(as_factor(data_source))) %>% 
  # mutate(data_source=factor(data_source, levels=c("Kuruc.info", "Alfahir.hu", "Press Releases",
                                                     # "Facebook - Vona page", "Facebook - Jobbik page"))) %>% 
  select(nativism, populism, data_source) %>% 
  group_by(data_source) %>% 
  mutate(nativism=mean(nativism, na.rm=TRUE),
         populism=mean(populism, na.rm=TRUE)) %>% 
  unique(.) %>% 
  ungroup(.) %>% 
  pivot_longer(cols=c(nativism, populism), names_to="type") %>% 
  arrange(type, value) %>% left_join(dat %>% select(-value))

dat_plot <- ggplot(dat_new, aes(x=reorder(data_source, -order), y=value, fill=type,alpha=type)) +
  geom_bar(stat="identity", position="dodge") +
  xlab("") + ylab("Share of words") +
  scale_fill_manual(values = c("nativism"="#000000","populism"="#002366")) +
  scale_alpha_manual(values=c(1,0.5))+
  theme_minimal() +
  theme(legend.position="bottom",legend.title=element_blank(),axis.text=element_text(size=8))

dat_plot

ggsave(filename="appd_figure1.png",
       path="figures",
       plot=dat_plot,
       device="png",
       dpi=300, 
       width=16,
       height=12,
       scale = 1.3,
       units="cm")
```

## Figure 2: The share of documents over time with a populist or nativist appeal on the different platforms (proportional)

```{r,fig.width=8,fig.height=12}
figure2 <- results_df_prop %>%
  ggplot(aes(x=date))+
  geom_smooth(span=0.2,aes(y=nativism,color="nativism", linetype="nativism",fill="nativism"),method="loess", size=0.6)+
  geom_smooth(span=0.2,aes(y=populism,color="populism", linetype="populism",fill="populism"),method="loess", size=0.6)+
  scale_x_date(breaks="years",date_labels="%Y")+
  #geom_smooth()+
  facet_wrap("data_source",ncol=2, nrow=3)+theme_minimal()+
  xlab("") + ylab("Share of words") +
  geom_vline(xintercept=c(as.Date("09.04.2006","%d.%m.%Y"),as.Date("11.04.2010","%d.%m.%Y"),as.Date("06.04.2014","%d.%m.%Y"),as.Date("08.04.2018","%d.%m.%Y")),linetype="dashed") +
  scale_colour_manual("",   limits=c("nativism", "populism"),
                      breaks = c("nativism", "populism"),
                      values = c("#002366", "black")) + 
  scale_fill_manual("",   limits=c("nativism", "populism"),
                      breaks = c("nativism", "populism"),
                      values = c("#002366", "black")) +
  scale_linetype_manual("",   limits=c("nativism", "populism"),
                        breaks = c("nativism", "populism"),
                        values = c("solid","dashed")) +
  guides(color=guide_legend(override.aes=list(fill=NA))) +
  theme(axis.title.x=element_blank(),
        axis.text.x = element_text(angle = 45, hjust = 1),
        legend.title = element_blank(),
        legend.position="bottom", 
        legend.direction="horizontal",
        legend.margin=margin(t = 0, unit='cm'),
        legend.key.width = unit(2.2,"cm"))+
  coord_cartesian(ylim=c(0,0.025))

figure2

ggsave(filename="appd_figure2.png",
       path="figures",
       plot=figure2,
       device="png",
       dpi=300, 
       width=13,
       height=16,
       scale = 1.3,
       units="cm")
```
